/**
 * 
 */
package AI;

import javax.microedition.khronos.opengles.GL10;

import orfeu.engine.*;
import hero.powers.*;

/**
 * @author Andriod Game Dev Team
 *
 */
public class Hero extends Sprite {

	//Variabile necesare eroului in joc
	public float HEALTH;
	public float SCORE;
	public float HIGHSCORE;
	private float RAGE;
	public float MAX_HEALTH = 800f;
	
	//variabile pentru proiectile
	public Projectile firstProjectile;
	private long last_time;
	public long proj_rate = 1500;//La cat timp sa apara un nou proiectil
	public float damage;
	
	
	private Powers powers;
	//Variabile de pozitie pt accelerometru si collision detection
	public float xPoz = 0;
	public float yPoz = 0;
	
	//Eroul este declarat ca si singleton
	private static Hero hero = new Hero(64,64,4,100,R.drawable.hero);
	
	
	private Hero(float w, float h, int nf, long fp, int res) {
		super(w, h, nf, fp, res);
		xPoz = 0;
		yPoz = 200;
		px += xPoz;
		py += yPoz;
		last_time = System.currentTimeMillis();
		damage = Constants.DEFAULT_HERO_DAMAGE;
		HEALTH = 8*Hell.HEALTH;
	}

	private Hero()
	{
		
	}

	public static Hero getHero()
	{
		return hero;
	}
	
	
	public boolean isAlive()
	{
		return (HEALTH > 0);
	}
 
	
	public boolean enRaged()
	{
		return false;
	}

	public static void reset()
	{
		hero = new Hero(64,64,4,100,R.drawable.hero);
	}
	
	public void setxPoz(float xPoz) {
		this.xPoz = xPoz;
		if(this.xPoz > Constants.limitRight) this.xPoz = Constants.limitRight;
		else if (this.xPoz < Constants.limitLeft) this.xPoz = Constants.limitLeft;
	}

	public float getxPoz() {
		return xPoz;
	}

	public void setyPoz(float yPoz) {
		this.yPoz = yPoz;
	}

	public float getyPoz() {
		return yPoz;
	}
	
	
	public void updateHealth(float damage)
	{
		this.HEALTH -= damage;
	}
	
	@Override
	/** update functions **/
	public void update(long time) {
		tick(time);
		
		// move object
		px += speed * interpolation;
		
		// keep object in some bounds
		if (px > 125.0f)
			px = 125.0f;
		
		
		//Cream un proiectil la un anumit interval de timp
		if(time >= last_time + proj_rate)
		{
			Projectile proiectil = new Projectile(16,16,1,100,R.drawable.nota2);
			proiectil.setPosition(this.xPoz, this.yPoz - 40);
			proiectil.setDamage(this.damage);
			proiectil.setSpeed(-3*Hell.SPEED);
			proiectil.textureId = firstProjectile.textureId;
			synchronized (GLView.getRenderer().drawList) {
				GLView.getRenderer().drawList.add(proiectil);
				GLView.getRenderer().drawList.notifyAll();
			}
			synchronized (GLView.getUpdater().updateList) {
				GLView.getUpdater().updateList.add(proiectil);
				GLView.getUpdater().updateList.notifyAll();
			}
			Hell.myProjectiles.add(proiectil);
			last_time = time;
		}
		
		
	}
	
	public void createHeroProjectile (float x, float y) {
		Projectile proiectil = new Projectile(16,16,1,100,R.drawable.nota2);
		proiectil.setPosition(x, y);
		proiectil.setDamage(this.damage);
		proiectil.setSpeed(-3*Hell.SPEED);
		proiectil.textureId = firstProjectile.textureId;
		synchronized (GLView.getRenderer().drawList) {
			GLView.getRenderer().drawList.add(proiectil);
			GLView.getRenderer().drawList.notifyAll();
		}
		synchronized (GLView.getUpdater().updateList) {
			GLView.getUpdater().updateList.add(proiectil);
			GLView.getUpdater().updateList.notifyAll();
		}
		Hell.myProjectiles.add(proiectil);
	}
	
	
	@Override
	/** draw function to display the quad **/
	public void draw(GL10 gl) {
		gl.glPushMatrix();
		gl.glFrontFace(GL10.GL_CCW);
		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glCullFace(GL10.GL_BACK);

		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glEnable(GL10.GL_TEXTURE_2D);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texcoordBuffer);
        gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
        
		gl.glTranslatef(px + xPoz, py, 0);
		gl.glRotatef(rx, 1, 0, 0);
		gl.glRotatef(ry, 0, 1, 0);
		gl.glScalef(sx, sy, 1);
				
		gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);

		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
		gl.glDisable(GL10.GL_CULL_FACE);
		gl.glPopMatrix();
	}
	
	@Override
	public boolean collidesWith(Sprite s){
		//myaudioclip.play();
		return (((this.px + this.xPoz - this.width/2) <= (s.px + s.width/2)) && 
				((this.px + this.xPoz + this.width/2) >= (s.px - s.width/2)) && 
				((this.py - this.height/2) <= (s.py + s.height/2)) &&
				((this.py + this.height/2) >= (s.py - s.height/2)));
	}
	
	public float getRage() {
		return this.RAGE;
	}
	
	public void setRage (float rage){
		this.RAGE = rage;
	}
}
